
package com.gw.business.common.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import com.gw.business.statistics.exception.model.EmailModel;
import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility;


/**
 * 邮件公用类
 * @author  kidy
 * @since   1.0
 * @version 2015年2月10日 kidy
 */
public class MailUtils {

   
    /** 
     * <br> 
     * 方法说明：把主题转换为中文 <br> 
     * 输入参数：String strText <br> 
     * 返回类型： 
     */  
    public String transferChinese(String strText) {  
        try {  
            strText = MimeUtility.encodeText(new String(strText.getBytes(),  
                    "GB2312"), "GB2312", "B");  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return strText;  
    }  
  
   
  
    /** 
     * <br> 
     * 方法说明：发送邮件 <br> 
     * 输入参数： <br> 
     * 返回类型：boolean 成功为true，反之为false 
     */  
    public static boolean sendMail( EmailModel email) {  
        
        
        JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
      //设定mail server 
          senderImpl.setHost(email.getHost());
          //建立邮件消息,发送简单邮件和html邮件的区别 
          MimeMessage mailMessage = senderImpl.createMimeMessage();
          MimeMessageHelper messageHelper=null;
        try {
            messageHelper = new MimeMessageHelper(mailMessage,true,"utf-8");
            //设置寄件人
            messageHelper.setFrom(email.getFromEmail());
            // 设置收件人邮箱  
            String[] toEmailArray = email.getToEmail().split(";");  
            List<String> toEmailList = new ArrayList<String>();  
            if (null == toEmailArray || toEmailArray.length <= 0) {  
                throw new RuntimeException("收件人邮箱不得为空！");  
            } else {  
                for (String emailTemp : toEmailArray) {  
                    
                    if (null!=emailTemp) {  
                        toEmailList.add(emailTemp);  
                    }  
                }  
                if (null == toEmailList || toEmailList.size() <= 0) {  
                    throw new RuntimeException("收件人邮箱不得为空！");  
                } else {  
                    toEmailArray = new String[toEmailList.size()];  
                    for (int i = 0; i < toEmailList.size(); i++) {  
                        toEmailArray[i] = toEmailList.get(i);  
                    }  
                }  
            }  
            messageHelper.setTo(toEmailArray);  
           
      
            //邮件主题  
            messageHelper.setSubject(email.getSubject());
            // true 表示启动HTML格式的邮件  
            messageHelper.setText(email.getContent(),true);
            senderImpl.setUsername(email.getUsername()); // 根据自己的情况,设置username
            senderImpl.setPassword(email.getPassword()); // 根据自己的情况, 设置password
            Properties prop = new Properties();
            prop.put("mail.smtp.auth", "true") ; // 将这个参数设为true，让服务器进行认证,认证用户名和密码是否正确
            prop.put("mail.smtp.timeout", "25000") ; 
            senderImpl.setJavaMailProperties(prop); 
            //发送邮件
            senderImpl.send(mailMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
       
  
    
        return true;  
    }  
  
  
      
    /** 
     * <br> 
     * 方法说明：主方法，用于测试 <br> 
     * 输入参数： <br> 
     * 返回类型： 
     */  
    public static void main(String[] args) {  
        MailUtils sendmail = new MailUtils();  
              
        //sendmail.setAttachments(attachments);
      /*  
        sendmail.setHost("smtp.163.com");  
        sendmail.setUserName("zhaojiaqun456@163.com");  
        sendmail.setPassWord("253667265");  
        sendmail.setTo("253667265@qq.com");  
        sendmail.setFrom("zhaojiaqun456@163.com");  
        sendmail.setSubject("你好，这是测试！");  
        sendmail.setContent("你好这是一个带多附件的测试！");  
        // Mail sendmail = new  
        // Mail("dujiang@sricnet.com","du_jiang@sohu.com","smtp.sohu.com","du_jiang","31415926","你好","胃，你好吗？");  
        sendmail.attachfile("d:\\aaa.rar");  
        */
          
   
  
    }  
}
